package nc.ui.cmp.originalbalance;

import java.awt.Container;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import nc.cmp.utils.AccountProxy;
import nc.cmp.utils.CmpUtils;
import nc.ui.pub.bill.BillModel;
import nc.vo.cmp.originalbalance.OriginalBalanceVO;
import nc.vo.pub.BusinessException;
import nc.vo.trade.pub.HYBillVO;
import nc.vo.uapbd.bankaccount.BankaccbasVO;

public class MngAccImportStrategy implements ImportStrategy {
	
	private Container container;
	
	private AccountProxy proxy;
	
	public void importData(Container container) throws BusinessException {
		setContainer(container);
		
		List<HYBillVO> mngList = getProxy().getMngList(getClientUI().getCurrentCorp());
		OriginalBalanceVO[] vos = getClientUI().getDelegate().getAll();
		if(vos == null || vos.length == 0) {
			import4Mng(mngList);
		}else {
			List<String> idList = CmpUtils.makeList();
			Map<String, HYBillVO> map = CmpUtils.makeMap();
			for(OriginalBalanceVO vo : vos) {
				if(vo.getPk_mngaccount() != null) {
					idList.add(vo.getPk_mngaccount());
				}
			}
			
			for(HYBillVO billVO : mngList) {
				BankaccbasVO bankVO = (BankaccbasVO) billVO.getParentVO();
				map.put(bankVO.getPrimaryKey(), billVO);
			}
			if(CmpUtils.isListNotNull(idList)) {
				for(String id : idList) {
					if(map.containsKey(id)) {
						map.remove(id);
					}
				}
				if(CmpUtils.isListNotNull(map.values())) {
					import4Mng(new ArrayList<HYBillVO>(map.values()));
				}
			}else {
				import4Mng(mngList);
			}
		}
		getClientUI().getBillCardPanel().getBillModel().execLoadFormula();
		
	}

	private void import4Mng(List<HYBillVO> mngList) {
		int rowCount = getClientUI().getBillCardPanel().getBillModel().getRowCount();
		for(HYBillVO billVO : mngList) {
			BankaccbasVO bankVO = (BankaccbasVO) billVO.getParentVO();
			if(bankVO.getAccopendate() == null) {
				continue;
			}
			getClientUI().getBillCardPanel().getBodyPanel().addLine();
			getClientUI().getBillCardPanel().getBillModel().setRowState(rowCount, BillModel.MODIFICATION);
			setValue(bankVO, rowCount);
//			executeEditFormula(rowCount);
			rowCount ++;
		}
	}
	
	private void setValue(BankaccbasVO vo, int i) {
		getClientUI().getBillCardPanel().setBodyValueAt(vo.getPrimaryKey(), i, "pk_mngaccount");
		getClientUI().getBillCardPanel().setBodyValueAt(vo.getMainaccount(), i, "pk_account");
		getClientUI().getBillCardPanel().setBodyValueAt(OriginalBalanceVO.BANK_DEPOSIT, i, "form_code");
		getClientUI().getBillCardPanel().setBodyValueAt(1, i, "formcode");
		getClientUI().getBillCardPanel().setBodyValueAt(getClientUI().getCurrentCorp(), i, "pk_corp");
		getClientUI().getBillCardPanel().setBodyValueAt(vo.getOwnercorp(), i, "pk_createcorp");
		getClientUI().getBillCardPanel().setBodyValueAt(vo.getAccopendate(), i, "makedate");
		getClientUI().getBillCardPanel().setBodyValueAt(getClientUI().getCurrentPerson(), i, "pk_operate");
		getClientUI().getBillCardPanel().setBodyValueAt(vo.getPk_currtype(), i, "pk_currtype");
		
	}
	
//	private void executeEditFormula(int i) {
//		getClientUI().getBillCardPanel().getBillModel().execEditFormulaByKey(i, "mngaccount");
//		getClientUI().getBillCardPanel().getBillModel().execEditFormulaByKey(i, "pk_operate");
//	}

	public Container getContainer() {
		return container;
	}

	public void setContainer(Container container) {
		this.container = container;
	}

	public AccountProxy getProxy() {
		if(proxy == null) {
			proxy = CmpUtils.makeObj(AccountProxy.class);
		}
		return proxy;
	}

	public void setProxy(AccountProxy proxy) {
		this.proxy = proxy;
	}
	

	public OriginalBalanceClientUI getClientUI() {
		return (OriginalBalanceClientUI)getContainer();
	}

}
